<?php

namespace App\Http\Controllers;

use App\Http\Requests\GetVersionRequest;
use App\Services\VersionService;
use Illuminate\Http\JsonResponse;
use Jiannei\Response\Laravel\Support\Facades\Response;

class VersionController extends Controller
{
    /**
     * Get Version
     * @param GetVersionRequest $request
     * @return JsonResponse
     */
    public function getVersion(GetVersionRequest $request): JsonResponse
    {
        try {
            $params = $request->only('version');
            $version = VersionService::getVersion();
            if(strcmp($version->version, $params['version'])) {
                $data = [
                    'code' => 1,
                    'msg' => 'ok',
                    'version' => $version->version,
                    'url' => $request->schemeAndHttpHost() . $version->url,
                    'log' => $version->log,
                    'force' => $version->force,
                ];
            } else {
                $data = [
                    'code' => 0,
                    'msg' => 'no',
                ];
            }
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }
}


